import { photoAlbumGroup, addPhotoAlbumGroup, photoAlbum, photoAlbumDetail, photo } from '@/api/content/operate'

const operate = {
    state: {
        groupList: [],//分组列表
        belongId: 0, //分组id


        albumList: [],//相册列表
        albumId: 0, //相册id
        albumInfo: {},//相册详情信息
        albumTotal: 0,//相册总数量
        isAlbumDel: false,//相册是否可删除
        albumCurrent: 1,//当前页码
        albumSize: 10, //当前显示条数

        photoList: [],//图片列表
        photoTotal: 0,//图片总数量
        isPhotoBatchDel: false, //图片是否可批量删除
        photoDelIds: [],//图片批量删除id数组
        photoCurrent: 1,//图片当前页码
        photoSize: 40, //图片当前显示条数
    },
    mutations: {
        SET_GROUPLIST: (state, groupList) => {
            state.groupList = groupList
        },
        SET_BELONGID: (state, belongId) => {
            state.belongId = belongId
        },
        SET_ALBUMLIST: (state, albumList) => {
            state.albumList = albumList
        },
        SET_ALBUMID: (state, albumId) => {
            state.albumId = albumId
        },
        SET_ALBUMINFO: (state, albumInfo) => {
            state.albumInfo = albumInfo
        },
        SET_ALBUMTOTAL: (state, total) => {
            state.albumTotal = total
        },
        SET_ISALBUMDEL: (state, isAlbumDel) => {
            state.isAlbumDel = isAlbumDel
        },
        SET_ALBUMCURRENT: (state, albumCurrent) => {
            state.albumCurrent = albumCurrent
        },
        SET_ALBUMSIZE: (state, albumSize) => {
            state.albumSize = albumSize
        },
        SET_PHOTOLIST: (state, photoList) => {
            state.photoList = photoList
        },
        SET_PHOTOTOTAL: (state, total) => {
            state.photoTotal = total
        },
        SET_ISPHOTOBATCHDEL: (state, isPhotoBatchDel) => {
            state.isPhotoBatchDel = isPhotoBatchDel
        },
        SET_PHOTODELIDS: (state, photoDelIds) => {
            state.photoDelIds = photoDelIds
        },
        SET_PHOTOCURRENT: (state, photoCurrent) => {
            state.photoCurrent = photoCurrent
        },
        SET_PHOTOSIZE: (state, photoSize) => {
            state.photoSize = photoSize
        },
    },
    actions: {
        //分组列表
        GetGroupList({ commit }, flag) {
            return new Promise((resolve, reject) => {
                photoAlbumGroup().then(response => {
                    let data = response.data || []
                    data.unshift({ id: undefined, name: '全部' })
                    let item = data[0]
                    commit('SET_GROUPLIST', data)
                    if (!flag) {
                        commit('SET_BELONGID', item ? item.id : undefined)
                    }
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        //新增分组
        AddPhotoAlbumGroup({ commit }, data) {
            return new Promise((resolve, reject) => {
                addPhotoAlbumGroup(data).then(response => {
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        //相册列表
        GetPhotoAlbum({ commit, state }, data = { current: 1, size: 10 }) {
            return new Promise((resolve, reject) => {
                data.current = state.albumCurrent
                data.size = state.albumSize
                photoAlbum({ belongId: state.belongId, ...data }).then(response => {
                    commit('SET_ALBUMLIST', response.data.records || [])
                    commit('SET_ALBUMTOTAL', response.data.total || 0)
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        //相册详情
        GetPhotoAlbumDetail({ commit, state }) {
            return new Promise((resolve, reject) => {
                photoAlbumDetail(state.albumId).then(response => {
                    commit('SET_ALBUMINFO', response.data || {})
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        //保存belongId
        SetBelongId({ commit }, data) {
            commit('SET_BELONGID', data)
        },
        //保存相册id
        SetAlbumId({ commit }, data) {
            commit('SET_ALBUMID', data)
        },
        //照片列表
        GetPhoto({ commit, state }, data = { current: 1, size: 10 }) {
            return new Promise((resolve, reject) => {
                data.current = state.photoCurrent
                data.size = state.photoSize
                photo({ belongId: state.albumId, ...data }).then(response => {
                    commit('SET_PHOTOLIST', response.data.records || [])
                    commit('SET_PHOTOTOTAL', response.data.total || 0)
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        //设置是否可批量删除图片
        SetIsPhotoBatchDel({ commit }, data) {
            commit('SET_ISPHOTOBATCHDEL', data)
        },
        //设置批量删除数组
        SetPhotoDelIds({ commit }, data) {
            commit('SET_PHOTODELIDS', data)
        },
    }
}

export default operate